﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraReports.UI;
using SmallERP.PinBan;
using Small.PinBan.PB.Presenter;
using Smallerp.Contracts.DataContracts;
using DevExpress.XtraReports.UI;
using Small.PinBan.PB.View;
using SmallERP.PinBan.PB.Presenter;

namespace Small.PinBan.FrmControl
{
    public partial class FrmPrint : Form
    {
        public FrmPrint(IPBMainView view)
        {
            InitializeComponent();
            _presenter = new PBMainPresenter(view);
        }

        private PBMainPresenter _presenter;

        private void Form1_Load(object sender, EventArgs e)
        {
            lbl_Mess.Text = "";
            lbl_photoCode.Text = "";

            
                  
            notifyIcon1.Visible = false;

            if (string.IsNullOrEmpty(PBDataTransfer.Completion))
            {
                print_completion.CheckState = CheckState.Checked;//默认选中
                PBDataTransfer.Completion = true.ToString();
            }
            else
            {
                if (Convert.ToBoolean(PBDataTransfer.Completion))
                {
                    print_completion.CheckState = CheckState.Checked;
                }
                else
                {
                    print_completion.CheckState = CheckState.Unchecked;
                }
            }
        }

        /// <summary>
        /// 打印
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void but_Print_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_Code.Text))
            {
                MessageBox.Show("请输入要打印的");
                return;
            }

            if (string.IsNullOrEmpty(PBDataTransfer.PrintName))
            {
                frmSelectPrintName printName = new frmSelectPrintName();
                printName.ShowDialog();
            }
            try
            {

                    //string oldtext = txt_Code.Text; //保存旧的
                    //int Stat = 3;
                    //if ((10 - txt_Code.Text.Length) > 0)
                    //{

                    //    int d = (10 - txt_Code.Text.Length - 1);
                    //    double dd = Math.Pow(10, d);
                    //    Stat = Convert.ToInt32(Stat * dd);
                    //    if (Stat == 1)
                    //    {
                    //        Stat = 3;
                    //    }
                    //    txt_Code.Text = Stat + txt_Code.Text;
                    //    lbl_photoCode.Text = txt_Code.Text;
                    //}

                PhotoInfo_PBDCT model = _presenter.GetPhotoInfo(Convert.ToInt32(txt_Code.Text.Trim()));

                if (model != null)
                    {
                        lbl_Mess.Text = "正在打印....";

                        //if (Convert.ToBoolean(PBDataTransfer.Completion))  //补充不足的
                        //{

                        //}
                        //else
                        //{
                        //    XtraReportProduct2 xrmLog = new XtraReportProduct2(oldtext, ordermodel.OrderPath, model.picPath, ordermodel.OrderProductCode, ordermodel.CtmName, ordermodel.photoInfo.Count(), ordermodel.OrderProductName);
                        //    xrmLog.Print(PBDataTransfer.PrintName);
                        //}

                        XtraReportProduct2 xrmLog = new XtraReportProduct2(model.PicPaths, model.OrderProductIds.ToString(), model.OrderName, model.PhotoIds.ToString(), model.PhotoNames);
                        xrmLog.Print(PBDataTransfer.PrintName);

                        lbl_Mess.Text = "打印完成";
                    }
                    else
                    {
                        txt_Code.Text = string.Empty;
                        lbl_Mess.Text = "没有找到条码信息";
                    }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            txt_Code.SelectAll();
        }

        /// <summary>
        /// 设置打印机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
           
                frmSelectPrintName printName = new frmSelectPrintName();
                printName.ShowDialog();
           
        }

        /// <summary>
        /// 扫描
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_Code_KeyPress(object sender, KeyPressEventArgs e)
        {           
            if (e.KeyChar == (int)Keys.Enter)
            {
                if (!string.IsNullOrEmpty(txt_Code.Text))
                {
                    but_Print_Click(null, null);
                }
            }
        }

        /// <summary>
        /// 设置打印机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 设置打印机ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSelectPrintName printName = new frmSelectPrintName();
            printName.ShowDialog();
           
        }

        string str = "";
        private void FrmPrint_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != (int)Keys.Enter)
            {
                str += e.KeyChar.ToString();
            }
            if (e.KeyChar == (int)Keys.Enter && !string.IsNullOrEmpty(str))
            {
                txt_Code.Text = str;
                lbl_photoCode.Text = str;
                if (!string.IsNullOrEmpty(txt_Code.Text))
                {
                    but_Print_Click(null, null);
                }
                str = string.Empty;
            }
        }

        /// <summary>
        /// 双击托盘
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
        
            //this.ShowInTaskbar = true;           
            //this.Show();
            //notifyIcon1.Visible = false;
            //this.TopMost = true;
        }

        /// <summary>
        /// 关闭窗体，隐藏到右下角
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmPrint_FormClosing(object sender, FormClosingEventArgs e)
        {
           
        //    this.Visible = false;            
        //    e.Cancel = true;                   
        //    notifyIcon1.Visible = true;
        //    this.ShowInTaskbar = false;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //DialogResult res = XtraMessageBox.Show("请确认是否退出程序？",
            //      "提示",
            //      MessageBoxButtons.OKCancel,
            //      MessageBoxIcon.Question);
            //if (res == DialogResult.OK)
            //{
            //    this.Close();
            //    this.Dispose();
            //    Application.Exit();
            //    Environment.Exit(-1);
            //}
            //else
            //{
            //    return;
            //}
        }

        private void 还原ToolStripMenuItem_Click(object sender, EventArgs e)
        {
          
            //this.ShowInTaskbar = true;
            //this.Show();
            //notifyIcon1.Visible = false;
            //this.TopMost = true;
        }

        private void 手动输入ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            txt_Code.Visible = !txt_Code.Visible;
        }

        /// <summary>
        /// 弹起
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txt_Code_KeyUp(object sender, KeyEventArgs e)
        {
            lbl_photoCode.Text = txt_Code.Text;
        }

      
        /// <summary>
        /// 是否补全
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 是否补全ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (print_completion.CheckState == CheckState.Checked)
            {
                PBDataTransfer.Completion = true.ToString();
            }

            else if (print_completion.CheckState == CheckState.Unchecked)
            {
                PBDataTransfer.Completion = false.ToString();
            }
            else
            {
                PBDataTransfer.Completion = "";
            }
        }
    }
}
